<template>
	<view>
		<scroll-view scroll-y class="page">
			<!-- 头部 -->
			<cu-custom bgColor="bg-rgb" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">申请报修</block>
			</cu-custom>
			<!-- 数据列表 -->
			<form @submit="handleSubmit">
				<view class="cu-form-group">
					<view class="title">客户编码：</view>
					<input placeholder="请输入客户编码" v-model:value="repairinfo.customerCode" name="customerCode"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">集研编码：</view>
					<input placeholder="请输入集研编码" v-model:value="repairinfo.researchCode" name="researchCode"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">测试工位：</view>
					<input placeholder="请输入测试工位" v-model:value="repairinfo.testStation" name="testStation"></input>
				</view>
				<view class="cu-form-group" v-if="!check">
					<view class="title">测试人员：</view>
					<input placeholder="请输入测试人员" name="firstTestPersonnel"></input>
				</view>
				<view class="cu-form-group" v-if="check">
					<view class="title">测试人员：</view>
					<input placeholder="请输入测试人员" name="retestPersonnel"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">对应条码（S)：</view>
					<input placeholder="请扫描对应条码（S)" v-model:value="repairinfo.correspondingBarcode" name="correspondingBarcode"></input>
				</view>
				<view class="cu-form-group">
					<view class="title">不合格现象：</view>
					<textarea placeholder="请输入不合格现象" v-model:value="repairinfo.unqualifiedPhenomenon" name="unqualifiedPhenomenon"/>
				</view>
				<button form-type="submit" class="btns cu-btn bg-rgb lg animation-fade">提交报修</button>
			</form>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				resultRadioCurrent: -1,
				hasGrade: false,
				type:'1',
				title:{
					'1':'首件',
					'2':'巡件',
					'3':'末件',
				},
				commandbillCode:'',
				produceId:'',
				check:false,
				repairinfo:{
					customerCode:'',
					researchCode:'',
					testStation:'',
					correspondingBarcode:'',
					unqualifiedPhenomenon:'',
				}
				
			}
		},
		onLoad(e) {
			this.produceId = this.$Route.query.produceId
			this.commandbillCode = this.$Route.query.commandbillCode
			this.check = this.$Route.query.check
			this.repairinfo = {...this.$Route.query.repairinfo}
			console.log(111,this.produceId,this.commandbillCode)
			// this.objMsg = uni.getStorageSync('firstPieceQualityInspection');
			// console.log(this.objMsg)
			// this.type = this.$Route.query.type
			// this.qualityTesting()
		},
		methods: {
			takephoto() {
				let that = this
				let maxUpload = 1
				uni.chooseImage({
					count: 1, // 最多可以选择的图片张数，默认1
					sizeType: ['original', 'compressed'], //original 原图，compressed 压缩图，默认二者都有
					sourceType: ['camera', 'album'], //album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
					success: (res) => {
						console.log(res);
						const tempFilePaths = res.tempFilePaths
						tempFilePaths.forEach(item => {
							uni.uploadFile({
								url: that.$http.config.baseUrl + 'sys/common/upload',
								filePath: item,
								method: 'POST',
								name: 'file',
								header: {
									'X-Access-Token': uni.getStorageSync('Access-Token')
								},
								success: function(uploadFileRes) {
									that.imglist = []
									that.modal.show({
										type: 'success',
										content: '上传成功!'
									})
									let result = JSON.parse(uploadFileRes.data)
									if (result.success) {
										that.imglist = that.imglist.concat([result
											.message]); //concat追加到数组
									} else {
										that.modal.show({
											type: 'error',
											content: result.message
										})
									}
								}
							});
						})
					}
				});
			},
			checkChange(item, evt){
				item.insLog = evt.target.value
			},
			radioChange: function(type, cur, evt) {
				for (let i = 0; i < this[type].length; i++) {
					if (this[type][i].value === evt.target.value) {
						this.cur = i;
						break;
					}
				}
			},
			checkboxChange: function (e) {
				var items = this.checkBoxItems,
					values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if(values.includes(item.value)){
						this.$set(item,'checked',true)
					}else{
						this.$set(item,'checked',false)
					}
				}
			},
			qualityTesting() {
				uni.showLoading({
					mask:true,
					title:'加载中.....'
				})
				let data = {
					inspectGrade: '201',
					materielCode: this.objMsg.mechanismCode
				}
				this.$http.get('inspect/mesMaterielCheckprojrct/getByGrade', {
					params: data
				}).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						this.list = res.data.result
						if (this.list.length > 0) {
							this.hasGrade =  true
							for (let i = 0; i < this.list.length; i++) {
								let data = {
									insLog: '',
									badCauses: '',
									insItem:this.list[i].projectName,
									// projectCode:this.list[i].projectCode
								}
								this.preservationList.push(data)
							}
							console.log(1111,this.preservationList)
						}
					} else {
						// this.$tip.error(res.data.message)
						this.modal.show({ type:'error', content:res.data.message })
						setTimeout(function() {
							uni.navigateBack({
								delta: 1
							})
						}, 1000);
					}
				})
			},
			handleSubmit(e) {
				let {customerCode, researchCode, testStation,firstTestPersonnel,retestPersonnel,correspondingBarcode,unqualifiedPhenomenon,} = e.detail.value
				let obj = {
					type:'repair_zs',
					commandbillCode:this.commandbillCode,
					produceId:this.produceId,
					...e.detail.value
				}
				console.log(111,obj)
				if (customerCode.trim().length === 0) {
					this.modal.show({ type:'error', content:'请输入客户编码' })
					return
				}	
				if (researchCode.trim().length === 0) {
					this.modal.show({ type:'error', content:'请输入集研编码' })
					return
				}	
				if (testStation.trim().length === 0) {
					this.modal.show({ type:'error', content:'请输入测试工位' })
					return
				}
				if(this.check){
					if (retestPersonnel.trim().length === 0) {
						this.modal.show({ type:'error', content:'请输入测试人员' })
						return
					}
				}else{
					if (firstTestPersonnel.trim().length === 0) {
						this.modal.show({ type:'error', content:'请输入测试人员' })
						return
					}
				}
				if (correspondingBarcode.trim().length === 0) {
					this.modal.show({ type:'error', content:'请输入对应条码' })
					return
				}
				if (unqualifiedPhenomenon.trim().length === 0) {
					this.modal.show({ type:'error', content:'请输入不合格现象' })
					return
				}	
				uni.showLoading({
					mask:true,
					title:'加载中.....'
				})
				//  let params = {
				// 	query1: this.imglist[0],
				// 	lineType,
				// 	productNum: commandbillCode,
				// 	productName: mechanismName,
				// 	productBom: mechanismCode,
				// 	firstPieceSmtlogs: this.preservationList,
				// 	...e.detail.value
				// }
				this.$http.post('inspect/zsUnqualifiedRepairCard/add', obj).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						this.modal.show({ type:'success', content:res.data.message })
						setTimeout(function() {
							uni.$emit('updateTest')
							uni.navigateBack({
								delta: 1
							})
						}, 1000);
					} else {
						this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
		},
	}
</script>

<style lang="less">
	.page {
		padding-bottom: 80rpx;
	}
	.borderTop {
		border-top: 0.5px solid #eee;
	}
	.border {
		border: 0.5px solid #eee;
	}
	.checkBoxWrap.cu-form-group+.cu-form-group {
		border: 0;
	}
	.btns{
		position: fixed;
		z-index: 999;
		background: white;
		bottom: 0;
		height: 80rpx;
		line-height: 100rpx;
	}
	.cu-btn{
		width: 650rpx;
		margin-left: 50rpx;
	}
	// .card {
	// 	margin: 30rpx;
	// 	box-shadow: 0px 0px 5rpx 2rpx rgba(0, 0, 0, 0.1);
	// 	border-radius: 10rpx;
	// 	padding: 30rpx;
	// 	background-color: white;
	// }

	// .card .top {
	// 	display: flex;
	// 	align-items: center;
	// 	justify-content: space-between;
	// 	border-bottom: 1rpx solid #F0F0F0;
	// 	padding-bottom: 20rpx;
	// 	font-weight: bold;
	// }

	// .card .bottom .box {
	// 	display: flex;
	// 	justify-content: space-between;
	// 	padding: 20rpx 0;
	// }

	// .card .bottom .box .lable {
	// 	width: 25%;
	// }

	// .card .bottom .box .content {
	// 	width: 74%;
	// 	text-align: right;
	// }
	// .boxs{
	// 	display: flex;
	// 	justify-content: space-between;
	// 	padding: 20rpx 0;
	// 	align-items: center;
	// }
	// // .boxs .title{
	// // 	font-weight: bold;
	// // 	padding: 10rpx;
	// // 	border-radius: 10rpx;
	// // 	color: white;;
	// // }
	// .boxs input {
	// 	border: 2rpx solid red;
	// 	height: 60rpx;
	// 	padding: 10rpx;
	// 	border-radius: 10rpx;
	// 	font-size: 20rpx;
	// 	color: red;
	// }
	// .boxs input .input-placeholder{
	// 	color: red;
	// }
	// uni-textarea{
	// 	border: 2rpx solid #F0F0F0;
	// 	padding: 10rpx;
	// 	border-radius: 10rpx;
	// 	font-size: 20rpx;
	// }
	// .list{
	// 	margin-bottom: 120rpx;
	// }
	// .btns{
	// 	position: fixed;
	// 	z-index: 999;
	// 	background: white;
	// 	bottom: 0;
	// 	height: 100rpx;
	// 	line-height: 100rpx;
	// }
	// .cu-btn{
	// 	width: 650rpx;
	// 	margin-left: 50rpx;
	// }
	.picture-box {
		width: 100%;
		margin-top: 10px;
	
		.picture {
			width: 100%;
			height: 200px;
			margin-bottom: 10px;
		}
	}
</style>
